/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.example.netcracker.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import ru.example.netcracker.FileSettings;
import ru.example.netcracker.player.Player;
import ru.example.netcracker.model.entity.Contact;
import ru.example.netcracker.model.entity.Task;
import ru.example.netcracker.model.ModelFacade;
import ru.example.netcracker.model.observer.Observer;
import ru.example.netcracker.player.PlayerFacade;
import ru.example.netcracker.timer.OnReceiveTaskListener;
import ru.example.netcracker.timer.ServiceTimer;

/**
 *
 * @author Nikolay
 */
public class TaskManagerController implements ControllerFacade {
    
    private static final Logger LOG = Logger.getLogger(TaskManagerController.class.getName());    
    
    private final ServiceTimer timer;
    private final ModelFacade model;
    private final PlayerFacade player;

    public TaskManagerController(ModelFacade listener) {
        this.model = listener;
        this.timer = new ServiceTimer();
        this.player = new Player();
        this.model.addObserver(timer);
    }
    
    @Override
    public void addOnReceiveTaskListener(OnReceiveTaskListener listener) {
        LOG.log(Level.INFO, "addOnReceiveTaskListener");
        if(timer != null) {            
            timer.addOnReceiveTaskListener(listener);
        }
    }
    
    @Override
    public void removeOnReceiveTaskListener() {
        LOG.log(Level.INFO, "removeOnReceiveTaskListener");
        if(timer != null) {            
            timer.removeOnReceiveTaskListener();
        }
    }

    @Override
    public void startPlayer(String path) {
        LOG.log(Level.INFO, "start player path: " + path);
        if (player != null) {
            File userAlarm = new File(path);
            if(userAlarm.exists()) {
                player.startPlayer(userAlarm);
            } else {
                FileSettings settings = FileSettings.getInstance();
                File defaultAlarm = settings.getDefaultAlarm();
                player.startPlayer(defaultAlarm);
            }
        }
    }

    @Override
    public void stopPlayer() {
        LOG.log(Level.INFO, "stop player");
        if (player != null) {            
            player.stopPlayer();
        }
    }

    @Override
    public void addObserver(Observer o) {
        LOG.log(Level.INFO, "Observer : " + o);
        if (model != null) {            
            model.addObserver(o);
        }
    }

    @Override
    public void deleteObserver(Observer o) {
        LOG.log(Level.INFO, "Observer: " + o);
        if (model != null) {            
            model.deleteObserver(o);
        }
    }

    @Override
    public List<Task> getTasks() {
        LOG.log(Level.INFO, "getTasks");
        if (model != null) {            
            return model.getTasks();
        }
        return null;
    }

    @Override
    public Task getTask(String id) {
        LOG.log(Level.INFO, "id: " + id);
        if (model != null) {            
            return model.getTask(id);
        }
        return null;
    }

    @Override
    public boolean createNotification(String name, String description, Date date) {
        LOG.log(Level.INFO, "name: " + name + " description: " + description + " date: " + date);
        if (model != null) {            
            return model.createNotification(name, description, date);
        }
        return false;
    }

    @Override
    public boolean createNotification(String name, String description, Date date, List<Contact> contacts) {
        LOG.log(Level.INFO, "name: " + name + " description: " + description + " date: " + date + " contacts: " + contacts);
        if (model != null) {
            model.createNotification(name, description, date, contacts);
        }
        return false;
    }
    
    @Override
    public boolean createAlarm(String name, String path, Date date) {
        LOG.log(Level.INFO, "name: " + name + " path: " + path + " date: " + date);
        if (model != null) {            
            return model.createAlarm(name, path, date);
        }
        return false;
    }    

    @Override
    public boolean updateNotification(String id, String name, String description, Date date, List<Contact> contacts) {
        LOG.log(Level.INFO, "id: " + id + " name: " + name + " description: " + description +
                " date: " + date + " contacts: " + contacts);
        if (model != null) {            
            return model.updateNotification(id, name, description, date, contacts);
        }
        return false;
    }
    
    @Override
    public boolean updateAlarm(String id, String name, String path, Date date) {
        LOG.log(Level.INFO, "id: " + id + " name: " + name + " path: " + path + " date: " + date);
        if (model != null) {
            return model.updateAlarm(id, name, path, date);
        }
        return false;
    }

    @Override
    public boolean deleteTask(String id) {
        LOG.log(Level.INFO, "id: " + id);
        if (model != null) {            
            return model.deleteTask(id);
        }
        return false;
    }
    
    @Override
    public void init() {
        LOG.log(Level.INFO, "init");
        if (model != null) {
            FileSettings settings = FileSettings.getInstance();
            model.setTaskProvider(settings.getTaskProvider());
            model.loadTasks();
            for (Task task : model.getTasks()) {
                timer.schedule(task);
            }
        }
    }

    @Override
    public void close() {
        LOG.log(Level.INFO, "close");
        stopPlayer();
        if (model != null) {
            model.saveTasks();
        }
    }
}
